package com.siyoumi.modules.account.service;

import com.siyoumi.component.XApp;
import com.siyoumi.component.XSpringContext;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.entity.SysStore;
import com.siyoumi.entity.SysStoreService;
import com.siyoumi.modules.account.vo.VoSysStore;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SvcSysStore {
    static public SvcSysStore getBean() {
        return XSpringContext.getBean(SvcSysStore.class);
    }

    static public SysStoreService getApp() {
        return XSpringContext.getBean(SysStoreService.class);
    }

    public List<Map<String, Object>> getList(String accId) {
        String[] select = {
                "store_id",
                "store_name",
        };
        JoinWrapperPlus<SysStore> query = listQuery();
        query.eq("store_acc_id", accId);
        query.select(select);

        return getApp().getMaps(query);
    }

    public JoinWrapperPlus<SysStore> listQuery() {
        return listQuery(InputData.getIns());
    }

    public JoinWrapperPlus<SysStore> listQuery(InputData inputData) {
        String name = inputData.input("name");

        JoinWrapperPlus<SysStore> query = getApp().join();
        query.eq("store_x_id", XHttpContext.getX())
                .eq("store_del", 0);
        //排序
        query.orderByDesc("store_id");
        if (XStr.hasAnyText(name)) { //名称
            query.like("store_name", name);
        }

        return query;
    }

    public XReturn edit(VoSysStore vo) {
        List<String> ignoreField = new ArrayList<>();
        InputData inputData = InputData.getIns();

        SysStore entityStore = getApp().getEntity(vo.getStore_id());
        if (entityStore != null) {
            inputData.put("id", vo.getStore_id());
        }

        return XApp.getTransaction().execute(status -> {
            XReturn r = getApp().saveEntity(inputData, vo, false, ignoreField);
            if (r.ok()) {
                SysStore entity = r.getData("entity");
                if (!entity.getStore_acc_id().equals(vo.getStore_acc_id())) { //帐号不同，清空帐号选中的商家
                    //更新acc_id
                    SysStore entityUpdate = new SysStore();
                    entityUpdate.setStore_id(entity.getKey());
                    entityUpdate.setStore_acc_id(vo.getStore_acc_id());
                    getApp().updateById(entityUpdate);

                    SvcAccount.getBean().clearStore(entityStore.getStore_acc_id());
                    SvcAccount.getBean().clearStore(vo.getStore_acc_id());
                }
            }
            return r;
        });
    }


    /**
     * 删除
     */
    @SneakyThrows
    @Transactional(propagation = Propagation.MANDATORY)
    public XReturn delete(List<String> ids) {
        XReturn r = XReturn.getR(0);

        getApp().delete(ids);

        return r;
    }
}
